package com.prs.crm.report;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.prs.crm.domain.hr.Person;

public class JxlExcel {
	public void export(String out, String title, List<String> items,List<Person> persons,Hashtable<?,?> hshData){
		try {
			OutputStream output = new FileOutputStream(out);
			WritableWorkbook workbook = Workbook.createWorkbook(output);
			WritableSheet sheet = workbook.createSheet(title, 0);
			renderBody(items, persons, hshData, sheet);
			workbook.write();
			workbook.close();
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void renderBody(List<String> items,List<Person> persons,Hashtable<?,?> hshData, WritableSheet sheet) throws Exception {
		int k = 0,j=1;
		for(String item : items){
			sheet.addCell(new Label(k++, 0, item));
		}
		
		for(int i = 0; i < persons.size(); i++){
			int t = 0;
			k = 0;
			for(String item : items){
				if(t == 0) {
					sheet.addCell(new Label(k++, j, persons.get(i).getResume().getName()));
					t++;
				}else{
					String key = persons.get(i).getId() + "$" + item;
					String value = hshData.get(key) + "";
					sheet.addCell(new Label(k++, j, value));
				}
			}
			j++;
		}
	}
}
